#include "planche.h"
#include <QPainter>

Planche::Planche(int width,int height,QWidget *parent) :
    QLabel(parent)
{
    down=false;
    polygon = new QPolygon();

    QRect rect(QPoint(10,10),QSize(width,height));
    setGeometry(rect);

    pixmap = new QPixmap(this->size());
    pixmap->fill();
    this->setPixmap(*pixmap);
}

/*
 *  Destructeur
 */
Planche::~Planche()
{
    delete(polygon);
    delete(pixmap);
}

Planche& Planche::operator=(const Planche& p )
{
   this->pixmap = p.pixmap;
   return *this;
}


void Planche::mousePressEvent(QMouseEvent *)
{
    down=true;
}

void Planche::mouseMoveEvent(QMouseEvent *ev)
{
    if(down)
    {
        QPixmap b(pixmap->size());
        b.fill();
        QPainter p2(&b);
        QPen pen(Qt::green, 5);
        p2.setPen(pen);

        QPolygon pol2(*polygon);
        pol2.insert(0,ev->pos());
        p2.drawPolygon(pol2);
        setPixmap(b);
    }
}

void Planche::mouseReleaseEvent(QMouseEvent *ev)
{
    down=false;

    QPainter p(pixmap);
    //enlever le fill pour garder l'historique
    pixmap->fill();
    QPen pen(Qt::black, 5);
    p.setPen(pen);

    p.drawPoint(ev->pos());
    polygon->insert(0,ev->pos());
    p.drawPolygon(*polygon);
    setPixmap(*pixmap);
}

